package redisson.rest;

import org.redisson.api.RIdGenerator;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
//@RestController
@RequestMapping("/id")
public class IdGeneratorController implements SmartInitializingSingleton {

	@Autowired
	RedissonClient redissonClient;

	RIdGenerator idGenerator;

	@GetMapping("/next")
	public String next() {

		for (int i = 0; i < 5000; i++) {
			idGenerator.nextId();
		}
		return "true";
	}

	@Override
	public void afterSingletonsInstantiated() {
		idGenerator = redissonClient.getIdGenerator("global:id");
		idGenerator.tryInit(1, 5000);
	}
}
